const getDefaultCpaasBabelrc = () => {
  const config = {
    presets: [['react-app', { absoluteRuntime: false }]],
    plugins: [
      ['use-const-enum', { importRegStr: 'choerodon-ui/.*/enum' }],
      [
        '@babel/plugin-proposal-decorators',
        {
          legacy: true,
        },
      ],
      [
        'import',
        {
          libraryName: 'cpaas-ui',
          libraryDirectory: 'es',
          style: false,
        },
        'ant',
      ],
      [
        'import',
        {
          libraryName: 'choerodon-ui',
          style: false,
        },
        'c7n',
      ],
      [
        'import',
        {
          libraryName: 'choerodon-ui/pro',
          style: false,
        },
        'c7n-pro',
      ],
      'lodash',
      [
        '@babel/plugin-proposal-class-properties',
        {
          loose: true,
        },
      ],
      process.env.DISABLE_AUTO_CSS_MODULES !== 'true' && [
        require.resolve('@umijs/babel-plugin-auto-css-modules'),
        {
          flag: 'modules',
        },
      ],
    ].filter(Boolean),
  };

  if (process.env.MULTIPLE_SKIN_ENABLE === 'true') {
    const uedConfig = require('cpaas-front/lib/utils/uedUtils');
    config.plugins = [
      ...uedConfig.generateCpaasUIConfig(),
      ...uedConfig.generateC7nUiConfig(),
      ...config.plugins.filter(
        ([_1, _2, pName] = []) => !['ant', 'c7n', 'c7n-pro'].includes(pName)
      ),
    ];
  }

  return config;
};

const getBabelrc = () => {
  const fs = require('fs');
  const paths = require('../paths');
  const path = require('path');
  let babelrcjsFile = path.resolve(process.cwd(), '.babelrc.js');
  if (!fs.existsSync(babelrcjsFile)) {
    babelrcjsFile = path.resolve(paths.appPath, '.babelrc.js');
    if (!fs.existsSync(babelrcjsFile)) {
      babelrcjsFile = path.resolve(paths.appRootPath, '.babelrc.js');
      if (!fs.existsSync(babelrcjsFile)) {
        return getDefaultCpaasBabelrc();
      }
    }
  }
  return require(babelrcjsFile);
};

module.exports = getBabelrc;
